package com.briup.mt.web.controller;

import com.briup.mt.bean.BasePrivilege;
import com.briup.mt.service.IBasePrivilegeService;
import com.briup.mt.utils.ExcelUtils;
import com.briup.mt.utils.Message;
import com.briup.mt.utils.MessageUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @auther zy
 * @date 2020/12/16 21:30
 */
@RestController
@Validated
@RequestMapping("/base_privilege")
public class BasePrivilegeController {
    @Autowired
    private IBasePrivilegeService basePrivilegeService;
    @ApiOperation(value = "导入到Excel中", notes = "注意！测试的时候请将地址粘贴到浏览器地址栏测试", produces = "application/octet-stream")
    @GetMapping("download")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query")
    })
    public void download(HttpServletResponse response, String name) throws Exception {
        // 2. 将excel导出
        // 2.1 excel头部数据
        String[] headList = new String[] { "姓名", "描述", "路径", "路径名称","类型","图标","隐藏","数字","上级id"};
        // 2.2 属性
        String[] fieldList = new String[] { "name", "description", "route", "route_name","type","icon","hidden","num","parent_id"};
        // 2.3 数据列，需要转化
        List<Map<String, Object>> dataList = new ArrayList<>();
        List<BasePrivilege> list= basePrivilegeService.query(name);
        for (BasePrivilege app : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", app.getId());
            map.put("name", app.getName());
            map.put("description", app.getDescription());
            map.put("route", app.getRoute());
            map.put("route_name", app.getRouteName());
            map.put("type", app.getType());
            map.put("icon", app.getIcon());
            map.put("hidden", app.getHidden());
            map.put("num", app.getNum());
            map.put("parent_id", app.getParentId());
            dataList.add(map);
        }
        // 2.4 导出文件名
        String excelName = "student_list";
        // 3. 调用工具方法导出Excel
        ExcelUtils.createExcel(response, excelName, headList, fieldList, dataList);

    }

    @GetMapping("query")
    public Message query(String name) {
        List<BasePrivilege> list = basePrivilegeService.query(name);
        return MessageUtil.success(list);
    }
    @PostMapping("saveOrUpdate")
    public Message SaveOrUpdate(BasePrivilege privilege){
        basePrivilegeService.saveOrUpdate(privilege);
        return MessageUtil.success("操作成功");
    }
    @DeleteMapping("deleteById")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value="用户编号",required =true, paramType = "query"),})
    public Message deleteById( long id){
        basePrivilegeService.deleteById(id);
    return MessageUtil.success("删除成功");
}
}
